from kiwi.core.base import Model, Instances, Instance

#*****************************************************************************
# PURPOSE: 
# USAGE:   
# PASSED ARGS:  
# RETURN ARGS: 
# Error - Fail Error Message
#*****************************************************************************
class TransUnit( Model ):
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************
	def __init__(  self, name = "TransUnit", type = "TransUnit" ):
		Model.__init__( self, name, type )
		self.initInDatasets()
		self.initOutDatasets()
	
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************	
	def initInDatasets( self ):
		self.params[ "INDATASETS" ] = []
			
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************
	def addInDatasets( self,  datasets ):
		if datasets is not None:
			self.params[ "INDATASETS" ].extend( datasets )
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************		
	def getInDatasets( self ):	
		return self.params[ "INDATASETS" ]
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************				
	def numOfInDataset( self ):
		return len( self.params[ "INDATASETS" ] )
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************		
	def initOutDatasets( self ):	
		self.params[ "OUTDATASETS" ] = []

	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************				
	def addOutDatasets( self, datasets ):	
		if datasets is not None:
			self.params[ "OUTDATASETS" ].extend( datasets )	

	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************				
	def getOutDatasets( self ):
		return self.params[ "OUTDATASETS" ]
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************				
	def numOfOutDataset( self ):
		return len( self.params[ "OUTDATASETS" ] )
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************	
	def setKeepType( self, keepType = 1  ):
		if keepType:
			keepType = 1
		else:
			keepType = 0
		self.params[ "KEEPTYPE" ] = keepType
	
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************	
	def getKeepType( self ):
		return self.params[ "KEEPTYPE" ]
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************			
	def doTransform( self ):	
		self.params[ "OUTDATASETS" ] = self.params[ "INDATASETS" ]
#*****************************************************************************
# PURPOSE: 
# USAGE:   
# PASSED ARGS:  
# RETURN ARGS: 
# Error - Fail Error Message
#*****************************************************************************		
class Transformer( TransUnit ):		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************
	def __init__(  self, name = "Transformer", type = "Transformer" ):
		TransUnit.__init__( self, name, type )
		self.initTransUnits()
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************
	def initTransUnits( self ):
		self.params[ "TRANSUNITS" ] = [] 	
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************
	def addTransUnits( self, transunits ):
		if transunits:
			self.params[ "TRANSUNITS" ].extend( transunits ) 
		
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************				
	def getTransUnits( self ):
		return self.params[ "TRANSUNITS" ]	

	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************		
	def numOfTransUnit( self ):
		return len( self.params[ "TRANSUNITS" ] )	
	
	#*****************************************************************************
	# PURPOSE: 
	# USAGE:   
	# PASSED ARGS:  
	# RETURN ARGS: 
	# Error - Fail Error Message
	#*****************************************************************************			
	def doTransform( self ):
		inDatasets = self.params[ "INDATASETS" ]
		if inDatasets:
			outDatasets = []
			keepType = self.getKeepType()
			for transunit in self.params[ "TRANSUNITS" ]:
				transunit.setKeepType( keepType )
				transunit.addInDatasets( inDatasets  )
				transunit.doTransform()
				outDatasets = transunit.getOutDatasets()
				inDatasets = outDatasets
			
			self.addOutDatasets( outDatasets )
			return outDatasets		
		else:
			return 0